package com.java复习.集合的学习;


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Setmain {
    static Set bookset = new HashSet();

    public static void main() {

        while (true){
            Scanner myScanner = new Scanner(System.in);
            System.out.println("=====图书管理系统=====");
            System.out.println("1.添加书籍（书号，书名）");
            System.out.println("2.查询全部数据");
            System.out.println("3.根据书号查书名");
            System.out.println("4.根据书号删除书籍");
            System.out.println("5.根据书号修改书名");
            System.out.println("6.退出系统");
            System.out.println("请输入你要选择的功能前的序号：");
            while(!myScanner.hasNextInt())
            {
                //没有这一行的话会导致无限循环
                String useless = myScanner.next();
                System.out.println("不是数字，重新输入");
            }
            //当是数字时循环结束
            int a = myScanner.nextInt();
            if(a>0 && a<6){
                a = a;
                switch (a){
                    case 1:
                        bookset.add(Setadd());
                        System.out.println(bookset);
                        break;
                    case 2:
                        showall();
                        break;
                    case 3:
                        find();
                        break;
                    case 4:
                        remove();
                        break;
                    case 5:
                        change();
                        break;

                }
            }else {
                System.out.println("===========退出系统===========");
                break;
            }

        }


    }

    public static Book Setadd(){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("输入要添加的书号：");
        int id = myScanner.nextInt();
        System.out.println("输入要添加的书名：");
        String name = myScanner.next();
        Book book = new Book();
        book.setId(id);
        book.setName(name);
        System.out.println("=====添加成功=====");
        return book;
    }

    public static void showall(){
//        Iterator<Book> it = booklist.iterator();
//        Book book;
        for (Object obj:bookset){
            System.out.println(obj);
        }
    }

    //根据书号查书名
    public static void find(){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要查找书籍的书号：");
        int iid = myScanner.nextInt();
        for (Object obj:bookset){
            if (iid == ((Book) obj).getId()){
                System.out.println("书籍号"+((Book) obj).getId()+"对应的"+"书籍名称为："+((Book) obj).getName());
                break;
            } else if (iid == 0) {
                System.out.println("请重新输入");
                break;
            }
        }
    }

    // 4.根据书号删除书籍

    public static void remove(){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要删除书籍的书号：");
        int iid = myScanner.nextInt();
        for (Object obj:bookset){
            if (iid == ((Book) obj).getId()){
                bookset.remove(obj);
                System.out.println("=====删除成功=====");
                break;
            } else if (iid == 0) {
                System.out.println("请重新输入");
                break;
            }
        }

    }


    // 5.根据书号修改书名
    public static void change(){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要修改书籍的书号：");
        int iid = myScanner.nextInt();
        System.out.println("请输入你要修改的书名：");
        String name = myScanner.next();
        for (Object obj:bookset){
            if (iid == ((Book) obj).getId()){
                ((Book) obj).setName(name);
                System.out.println("书籍号"+((Book) obj).getId()+"对应的"+"书籍名称修改为："+((Book) obj).getName());
                break;
            }
        }
    }
}

